package com.cooldatasoft.menu.modal;

import java.util.Date;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

import com.cooldatasoft.app.WOCApplication;
import com.cooldatasoft.app.WOCSession;
import com.cooldatasoft.data.ComboValue;
import com.cooldatasoft.data.Node;

public class AddSubGroupModalPanel extends Panel {

	
	private ModalWindow currentModal;
	//FIXME bu element sayfada yok
	private FeedbackPanel feedbackPanel;
	private Integer parentNodeId;
	
	public AddSubGroupModalPanel(ModalWindow currentModal, Integer parentNodeId) {
		
		super(currentModal.getContentId());
		this.currentModal=currentModal;
		MarkupContainer container= new WebMarkupContainer("container");		
		MarkupContainer header = new WebMarkupContainer("header");
		Label companyName = new Label("companyName","Company Name");
		header.add(companyName);
		container.add(header);		
		
		MarkupContainer wrapper = new WebMarkupContainer("wrapper");
		MarkupContainer personalInfoDiv = new WebMarkupContainer("personalInfoDiv");
		PersonPropertyForm personalInfoForm = new PersonPropertyForm("personalInfoForm");
		personalInfoDiv.add(personalInfoForm);
		wrapper.add(personalInfoDiv);
		
		container.add(wrapper);
		setParentNodeId(parentNodeId);
		add(container);
		setOutputMarkupId(true);
	}

	



	class PersonPropertyForm extends Form{

		private Node node;
		private ComboValue managerComboValue;
		
		public PersonPropertyForm(String id) {
			super(id);
			
			Label idLabel = new Label("idLabel","Id");
			Label labelLabel = new Label("labelLabel","Label");
			Label managerLabel = new Label("managerLabel","Manager");
			
			add(idLabel);
			add(labelLabel);
			add(managerLabel);
			
			
			
			Label idValue = new Label("idValue",new PropertyModel(this,"node.id"));
			TextField labelTextField = new TextField("labelTextField",new PropertyModel(this,"node.label"));
			List<ComboValue> managerOptionlist = WOCApplication.get().getPersonServiceProxy().getPersonSelectOptions();
			ChoiceRenderer choiceRenderer = new ChoiceRenderer("value", "key");
			DropDownChoice managerDropDown = 
				new DropDownChoice("managerDropDown", new PropertyModel(this, "managerComboValue"), managerOptionlist, choiceRenderer);
			
//			List<ComboValue> nodeOptionlist = WOCApplication.get().getNodeServiceProxy().getNodeSelectOptions();
//			DropDownChoice parentDropDown = 
//				new DropDownChoice("parentNodeDropDown", new PropertyModel(this, "parentNodeComboValue"), nodeOptionlist, choiceRenderer);
			
			
			add(managerDropDown);
//			add(parentDropDown);
			add(idValue);
//			add(dataTypeDropDownChoice);
			add(labelTextField);				
			
			
			
			AjaxButton ajaxButton = new AjaxButton("saveButton",this) {
				@Override
				protected void onSubmit(AjaxRequestTarget target, Form form) {
					getNode().setNodeType("STRING");
					getNode().setUpdateDate(new Date());
					getNode().setCreateUserId(WOCSession.get().getUser().getId());
					getNode().setUpdateUserId(WOCSession.get().getUser().getId());
					getNode().setManagerId(getManagerComboValue().getKey());
					
					if(getParentNodeId()!=null) {
						getNode().setParentId(getParentNodeId());
					}else {
						getNode().setParentId(1);
					}
					
					WOCApplication.get().getNodeServiceProxy().saveOrUpdate(getNode());
					
					AddSubGroupModalPanel.this.currentModal.close(target);
				}
				
			};
			
			
			
			
			add(ajaxButton);			
		}

		public Node getNode() {
			return node;
		}

		public void setNode(Node node) {
			this.node = node;
		}

		public ComboValue getManagerComboValue() {
			return managerComboValue;
		}

		public void setManagerComboValue(ComboValue managerComboValue) {
			this.managerComboValue = managerComboValue;
		}
		
	}





	public Integer getParentNodeId() {
		return parentNodeId;
	}





	public void setParentNodeId(Integer parentNodeId) {
		this.parentNodeId = parentNodeId;
	}
}
